﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using IFactory;

public partial class QuarterPlan_QuarterPlanResolveDetail : System.Web.UI.Page
{
    QuarterDateManager dllQuarterDateManager = new QuarterDateManager();
    QuarterPlanofManager dllQuarterPlanofManager = new QuarterPlanofManager();
    UserInfoManager dllUserInfoManager = new UserInfoManager();
    QuarterPlanofManager dllQuarterPlan = new QuarterPlanofManager();
    QuarterPlanDetailManager dllQuarterPlanDetail = new QuarterPlanDetailManager();
    Authentication dllAuthentication = new Authentication();
    IBussinessLogic dllUserInfo = new UserInfoManager();
    DepartmentInfoManager dllDepartment = new DepartmentInfoManager();
    Model.UserInfo objUserInfo = new Model.UserInfo();
    DataTable _powerTable = new DataTable();
    public string _quarterPlanSortId = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        dllAuthentication.isLogin();
        if (!IsPostBack)
        {
            bindGridViewQuarterResolve();
        }
    }

    /// <summary>
    /// 自动分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvlist_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gdvQuarterResolve.PageIndex = e.NewPageIndex;
        bindGridViewQuarterResolve();
    }



    /// <summary>
    /// 绑定年分解计划GridView
    /// </summary>
    void bindGridViewQuarterResolve()
    {
        object[] _value = new object[1];
        _value[0] = Utility.HttpHelp.getParamsString("_quarterPlanId");       
        DataTable _quarterResolve = dllQuarterPlanDetail.get(QuarterPlanDetailManager.field.QuarterPlanId.ToString(), _value);
        this.gdvQuarterResolve.DataSource = _quarterResolve;
        this.gdvQuarterResolve.DataBind();
        for (int i = 0; i < this.gdvQuarterResolve.Rows.Count; i++)
        {            
            wrapByComma(this.gdvQuarterResolve, i, 5);
            wrapByComma(this.gdvQuarterResolve, i, 6);
            if (this.gdvQuarterResolve.Rows[i].Cells[10].Text == "&amp;nbsp;" || this.gdvQuarterResolve.Rows[i].Cells[10].Text == "&nbsp;")
            {
                this.gdvQuarterResolve.Rows[i].Cells[10].Text = "";
            }
        }
    }


    /// <summary>
    /// 根据逗号换行事件
    /// </summary>
    void wrapByComma(GridView _gridView, int _rowIndex, int _cells)
    {
        string _content = _gridView.Rows[_rowIndex].Cells[_cells].Text;
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&lt;br&gt;", "<br>");
        //_gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&gt;", ">");
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _gridView.Rows[_rowIndex].Cells[_cells].Text.Replace(",", "<br>");


    }

    /// <summary>
    /// 修改按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnModify_Click(object sender, EventArgs e)
    {
        int _quarterPlanDetailId = 0;
        int _quarterPlanId = 0;
        int _count = 0;
        string _sponsor = "";
        for (int i = 0; i < this.gdvQuarterResolve.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvQuarterResolve.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked)
            {
                _count++;
                _quarterPlanDetailId = Convert.ToInt32(this.gdvQuarterResolve.DataKeys[i]["Id"].ToString());
                _quarterPlanId = Convert.ToInt32(this.gdvQuarterResolve.DataKeys[i]["QuarterPlanId"].ToString());
                _sponsor = this.gdvQuarterResolve.DataKeys[i]["Sponsor"].ToString();
            }
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请选择一个要修改的年分解计划", Page);
        }
        if (_count > 1)
        {
            Utility.HttpHelp.alert("只能选择一个要修改的年分解计划", Page);
        }
        if (_count == 1)
        {
            if (objUserInfo.UserName == _sponsor || objUserInfo.UserName == "admin")
            {
                object[] _value = new object[1];
                _value[0] = _quarterPlanId;
                DataTable _quarterPlanTable = dllQuarterPlan.get(QuarterPlanofManager.field.QuarterPlanId.ToString(), _value);
                if (_quarterPlanTable.Rows.Count > 0)
                {
                    if (Convert.ToInt32(_quarterPlanTable.Rows[0]["PlanSendState"].ToString()) == 0 || objUserInfo.UserName == "admin" || Convert.ToInt32(_quarterPlanTable.Rows[0]["PlanSendState"].ToString()) == -1)
                    {
                        string _url = "ModifyQuarterPlanDetail.aspx?Id=" + _quarterPlanDetailId;
                        Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','ModifyQuarterPlanResolveDetail','height=200,width=900,top=400,left=80,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
                    }
                    else
                    {
                        Utility.HttpHelp.alert("该年分解所属的年计划已提交，不可以修改！", Page);
                    }
                }
            }
            else
            {
                Utility.HttpHelp.alert("发起人或计划管理员才可以执行修改操作", Page);
            }

        }

        #region
        //int _count = 0;
        ////int _quarterPlanId = 0;
        //int _resolveDetailId = 0;
        //for (int i = 0; i < this.gdvQuarterResolve.Rows.Count; i++)
        //{
        //    CheckBox ckb = (CheckBox)this.gdvQuarterResolve.Rows[i].Cells[0].FindControl("ckbSelect");
        //    if (ckb.Checked == true)
        //    {
        //        _count++;
        //       // _quarterPlanId = Convert.ToInt32(this.gdvQuarterResolve.DataKeys[i]["QuarterPlanId"].ToString());
        //        _resolveDetailId = Convert.ToInt32(this.gdvQuarterResolve.Rows[i].Cells[1].ToString());
        //    }
        //}
        //if (_count == 1)
        //{
        //    //判断是否为评估人，如果该条计划的评估人是当前用户则不能修改该条计划

        //    Model.UserInfo objUser = new Model.UserInfo();
        //    object[] _user = new object[1];
        //    _user[0] = objUser.UserName;
        //    DataTable _userTable = dllUserInfo.get(UserInfoManager.field.UserName.ToString(), _user);
        //    if (_userTable.Rows.Count > 0)
        //    {
        //        object[] _username = new object[2];
        //        _username[0] = _userTable.Rows[0]["RealName"].ToString();
        //        _username[1] = _resolveDetailId;
        //        DataTable _iSEvaluatePeople = dllQuarterPlanofManager.get(QuarterPlanofManager.field.EvaluatePeople.ToString(), QuarterPlanofManager.field.QuarterPlanId.ToString(), _username);
        //        if (_iSEvaluatePeople.Rows.Count > 0)
        //        {
        //            Utility.HttpHelp.alert("评估人不能修改计划", Page);
        //        }
        //        else
        //        {
        //            string _url = "ModifyQuarterPlanResolveDetail.aspx?_resolveDetailId=" + _resolveDetailId;
        //                Page.RegisterStartupScript("windowopen", "<script>window.open('" + _url + "','','height=160,width=884,top=300,left=100,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');</script>");
        //                return;                   
        //        }
        //    }

        //}
        //else
        //{
        //    Utility.HttpHelp.alert("只能选择一个", Page);
        //}
        #endregion
    }


    /// <summary>
    /// 删除按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDel_Click(object sender, EventArgs e)
    {
        string _alert = "";
        int _count = 0;
        string _sponsor = "";
        List<object[]> _valueList = new List<object[]>();
        for (int i = 0; i < this.gdvQuarterResolve.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvQuarterResolve.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked)
            {
                _count++;
                object[] _quarterPlanId = new object[1];
                _quarterPlanId[0] =  Convert.ToInt32(this.gdvQuarterResolve.DataKeys[i]["QuarterPlanId"].ToString());
                _sponsor = this.gdvQuarterResolve.DataKeys[i]["Sponsor"].ToString();
                DataTable _quarterPlanTable = dllQuarterPlan.get(QuarterPlanofManager.field.QuarterPlanId.ToString(), _quarterPlanId);
                if (_quarterPlanTable.Rows.Count > 0)
                {
                    //if (Convert.ToInt32(_quarterPlanTable.Rows[0]["PlanSendState"].ToString()) == 0||Convert.ToInt32(_quarterPlanTable.Rows[0]["PlanSendState"].ToString()) == -1)
                    //{
                        object[] _value = new object[1];
                        _value[0] = Convert.ToInt32(this.gdvQuarterResolve.DataKeys[i]["Id"].ToString());
                        _valueList.Add(_value);
                    //}
                    //else
                    //{
                    //    _alert += this.gdvQuarterResolve.Rows[i].Cells[1].Text+",";
                    //}
                }
            }
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请选择要删除的记录", Page);
        }
        if (_alert.Length > 0)
        {
            _alert = _alert.Remove(_alert.Length - 1, 1);
        }
        if (_valueList.Count > 0)
        {
            if (objUserInfo.UserName == _sponsor || objUserInfo.UserName == "admin")
            {
                int result = dllQuarterPlanDetail.delWithTransaction(_valueList);
                if (result == 0)
                {
                    Utility.Error.RedirectErrorPage("删除年分解计划未成功，请重试！");
                }
                else
                {
                    if (_alert.Length > 0)
                    {
                        Utility.HttpHelp.alert("计划编号为" + _alert + "的计划不可以删除，因为对应的年计划已经提交", Page);
                    }
                    bindGridViewQuarterResolve();
                }
            }
            else
            {
                Utility.HttpHelp.alert("发起人或计划管理员才可以执行删除操作", Page);
            }
        }
        if (_alert.Length > 0)
        {
            Utility.HttpHelp.alert("计划编号为" + _alert + "的计划不可以删除，因为对应的年计划已经提交", Page);
        }
    }


    /// <summary>
    /// 返回事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Page.RegisterStartupScript("re", "<script>window.opener.location.href=window.opener.location.href;self.close();</script>");
        //Page.RegisterStartupScript("refresh", "<script language=javascript>if(window.opener){window.opener.location.reload();window.opener = null;window.close();} ");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        object[] _value = new object[1];
        _value[0] = Utility.HttpHelp.getParamsInt("_quarterPlanId");
        DataTable _quarterPlan = dllQuarterPlan.get(QuarterPlanofManager.field.QuarterPlanId.ToString(), _value);
        if (_quarterPlan.Rows.Count > 0)
        {
            _quarterPlanSortId = _quarterPlan.Rows[0]["QuarterPlanSortId"].ToString();
        }
        string _url = "QuarterResolveAdminAdd.aspx?_quarterPlanId=" + Utility.HttpHelp.getParamsString("_quarterPlanId") + "&_quarterPlanSortId="+_quarterPlanSortId;
        Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','quarterplandetailadminmodify','height=200,width=902,top=300,left=80,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no');</script>");
    }
}
